package com.zbbt.sportslottery.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.ui.adapter.GuidePagerAdapter;
/**
 * 
 * @ClassName: GuideActivity
 * @Description: 引导页
 * @author：LiZhimin
 * @date：2014-12-9 下午2:56:16     
 * @version V1.0
 */
public class GuideActivity extends BaseActivity {

	private final int[] resIds = new int[] { R.drawable.splash_1, R.drawable.splash_2};

	private ViewPager mGuidePage;

	private List<View> mGuideViews = new ArrayList<View>();

	private LinearLayout mStartApp;

	private int mLastScollState = -1;

	private int mCurrentPage = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		initGuideView();
	}

	/**
	 * 初始化引导页
	 */
	private void initGuideView() {

		mGuidePage = (ViewPager) findViewById(R.id.guidePager);
		mStartApp = (LinearLayout) findViewById(R.id.start_app);
		for (int i = 0; i < resIds.length; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setImageResource(resIds[i]);
			ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT);
			imageView.setLayoutParams(params);
			imageView.setScaleType(ScaleType.FIT_XY);
			mGuideViews.add(imageView);
		}
		mGuidePage.setAdapter(new GuidePagerAdapter(mGuideViews));
		for(int i=0;i<resIds.length-1;i++){
			final int index=i;
			mGuideViews.get(i).setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					mGuidePage.setCurrentItem(index+1);
					
				}
			});
			
		}
		mGuidePage.setOnPageChangeListener(new OnPageChangeListener() {

			public void onPageSelected(int position) {

				mCurrentPage = position;
				if (resIds.length - 1 == position) {
					mStartApp.setVisibility(View.VISIBLE);
				} else {
					mStartApp.setVisibility(View.GONE);
				}
			}

			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			public void onPageScrollStateChanged(int arg0) {
				if (mLastScollState == 1 && arg0 == 0 && mCurrentPage == (resIds.length - 1)) {// 最后一页向右滑动了
				}
				mLastScollState = arg0;
			}

		});

		mStartApp.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				goMainTab();
			}
		});
	}
     private void goMainTab(){
    	 goActivity(MainActivity.class);
    	 finish();
     }
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			goMainTab();
			return true;
		}
		return false;
	}
}
